package wpRelations;

import processing.core.PApplet;

/* code from Ben Fry's Visualizing Data;
 * 
 */


public class TsvTable {
	PApplet parent;
	
		  int rowCount;
		  public String[][] data;
		  
		  
		 public TsvTable(PApplet parent, String filename) {
			 this.parent =parent;
		    String[] rows = parent.loadStrings(filename);
		    data = new String[rows.length][];
		    
		    for (int i = 0; i < rows.length; i++) {
		      if (parent.trim(rows[i]).length() == 0) {
		        continue; // skip empty rows
		      }
		      if (rows[i].startsWith("#")) {
		        continue;  // skip comment lines
		      }
		      
		      // split the row on the tabs
		      String[] pieces = parent.split(rows[i], parent.TAB);
		      // copy to the table array
		      data[rowCount] = pieces;
		      rowCount++;
		      
		      // this could be done in one fell swoop via:
		      //data[rowCount++] = split(rows[i], TAB);
		    }
		    // resize the 'data' array as necessary
		    data = (String[][]) parent.subset(data, 0, rowCount);
		  }
		  
		  
		  public int getRowCount() {
		    return rowCount;
		  }
		  
		  
		  // find a row by its name, returns -1 if no row found
		  public int getRowIndex(String name) {
		    for (int i = 0; i < rowCount; i++) {
		      if (data[i][0].equals(name)) {
		        return i;
		      }
		    }
		    System.out.println("No row named '" + name + "' was found");
		    return -1;
		  }
		  
		  
		  public String getRowName(int row) {
		    return getString(row, 0);
		  }


		  public String getString(int rowIndex, int column) {
		    return data[rowIndex][column];
		  }

		  
		  public String getString(String rowName, int column) {
		    return getString(getRowIndex(rowName), column);
		  }

		  
		  public int getInt(String rowName, int column) {
		    return parent.parseInt(getString(rowName, column));
		  }

		  
		  public int getInt(int rowIndex, int column) {
		    return parent.parseInt(getString(rowIndex, column));
		  }

		  
		  public float getFloat(String rowName, int column) {
		    return parent.parseFloat(getString(rowName, column));
		  }

		  
		  public float getFloat(int rowIndex, int column) {
		    return parent.parseFloat(getString(rowIndex, column));
		  }
		  
		  
		  public void setRowName(int row, String what) {
		    data[row][0] = what;
		  }


		  public void setString(int rowIndex, int column, String what) {
		    data[rowIndex][column] = what;
		  }

		  
		  public void setString(String rowName, int column, String what) {
		    int rowIndex = getRowIndex(rowName);
		    data[rowIndex][column] = what;
		  }

		  
		  public void setInt(int rowIndex, int column, int what) {
		    data[rowIndex][column] = parent.str(what);
		  }

		  
		  public void setInt(String rowName, int column, int what) {
		    int rowIndex = getRowIndex(rowName);
		    data[rowIndex][column] = parent.str(what);
		  }

		  
		  public void setFloat(int rowIndex, int column, float what) {
		    data[rowIndex][column] = parent.str(what);
		  }


		  public void setFloat(String rowName, int column, float what) {
		    int rowIndex = getRowIndex(rowName);
		    data[rowIndex][column] = parent.str(what);
		  }  
		}
